package com.lgx.mall.business.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.entity.Dictionary;
import com.lgx.common.web.Constants;
import com.lgx.common.web.Pagination;
import com.lgx.common.web.json.Json2Utils;
import com.lgx.common.web.util.DictUtils;
import com.lgx.common.web.util.EscapeUtils;
import com.lgx.mall.business.entity.Order;
import com.lgx.mall.business.entity.OrderDetail;
import com.lgx.mall.business.entity.ReturnRequest;
import com.lgx.mall.business.service.IOrderDetailService;
import com.lgx.mall.business.service.IOrderService;
import com.lgx.mall.business.service.IReturnRequestService;
import com.lgx.mall.core.entity.Product;
import com.lgx.mall.core.service.IProductService;

/**
 * 后台操作的订单Controller类，执行权限为admin或者是其他拥有后台操作权限的用户
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/order")
public class OrderController {
	
	@Autowired
	private IOrderService orderService;
	
	@Autowired
	private IOrderDetailService orderDetailService;   //订单商品
	
	@Autowired
	private IProductService productService;           //商品
	
	@Autowired
	private IReturnRequestService requestService;     //退货记录
	
	
	
	public IReturnRequestService getRequestService() {
		return requestService;
	}


	public void setRequestService(IReturnRequestService requestService) {
		this.requestService = requestService;
	}


	public IProductService getProductService() {
		return productService;
	}


	public void setProductService(IProductService productService) {
		this.productService = productService;
	}


	public IOrderDetailService getOrderDetailService() {
		return orderDetailService;
	}


	public void setOrderDetailService(IOrderDetailService orderDetailService) {
		this.orderDetailService = orderDetailService;
	}


	public IOrderService getOrderService() {
		return orderService;
	}


	public void setOrderService(IOrderService orderService) {
		this.orderService = orderService;
	}

	/**
	 * orderMgr.do --我的订单跳转
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/orderMgr.do")
	public String orderMgr(HttpServletRequest request,HttpServletResponse response){
		return "/mall/business/order/orderMgr";
	}
	
	
	@RequestMapping("/list.do")
	public void list(HttpServletRequest request,HttpServletResponse response){
		//接受必要参数
		String username = request.getParameter("username");
		String usercard = request.getParameter("usercard");
		String orderid = request.getParameter("orderid");  //订单号
		String starTime = request.getParameter("starTime");
		String endTime = request.getParameter("endTime");
		String status = request.getParameter("status");
		
		String pageNum = request.getParameter("page");
		String pageSize= request.getParameter("rows");
		
		//加载订单状态数据字典
		List<Dictionary> orderStatus = DictUtils.getDictListByKind(DictUtils.ORDER_TYPE);
		request.setAttribute("orderStatus", orderStatus);
		
		
		if(StringUtils.isEmpty(pageSize))pageSize = Constants.PAGE_SIZE;
		if(StringUtils.isEmpty(pageNum))pageNum = Constants.FIRST_PAGE_NUM;
		
		Pagination page = this.orderService.findOrderPagination(username, usercard, orderid, starTime, endTime,status,Integer.parseInt(pageSize), Integer.parseInt(pageNum));
		Json2Utils.parseJSON(page, response);
	}
	
	/**
	 * 查看详细订单信息
	 * 查看订单详细与直接点击订单号均可调用实现跳转
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findOrderUrl.do")
	public String findOrderUrl(HttpServletRequest request,HttpServletResponse response){
		String orderNo = request.getParameter("orderNo");
		Order order = this.orderService.findOrderByOrderNo(orderNo);
		if(order!=null){
			request.setAttribute("order", order);
		}
		return "/mall/business/order/order-init";
	}
	
	/**
	 * 加载订单详情
	 */
	@RequestMapping("/orderDetails.do")
	public String orderDetails(HttpServletRequest request,HttpServletResponse response){
		String orderNo = request.getParameter("orderNo");
		Order order = this.orderService.findOrderByOrderNo(orderNo);
		List<OrderDetail> lDetails = this.orderDetailService.findOrderDetailList(order.getId());
		Double weight = 0.0;   //定义商品总重量
		//计算商品总总量
		for (OrderDetail orderDetail : lDetails) {
			Product product =  this.productService.findProductById(orderDetail.getEntityId());
			if(product!=null){
				weight += product.getWeight();
			}
		}
		
		//得到配送方式数据字典
		List<Dictionary> fareways = DictUtils.getDictListByKind(DictUtils.PRODUCT_FAREWAY);
		request.setAttribute("fareways", fareways);
		
		if(order!=null&&lDetails.size()>0){
			request.setAttribute("order", order);
			request.setAttribute("weight", weight);
		}
		return "/mall/business/order/orderDetails";
	}
	
	/**
	 * 加载订单商品信息
	 * 这里有一个类型判断，如果是有退货情况的，给予一个指定的参数，如果没有的，则不需要
	 */
	@RequestMapping("/orderProduct.do")
	public String orderProduct(HttpServletRequest request,HttpServletResponse response){
		String orderNo = request.getParameter("orderNo");
		/*String type = request.getParameter("type");
		String url = "";
		if(type.equals("findproduct")){
			url = "/mall/business/order/orderProduct";
		}else{
			url = "/mall/business/order/return-Order";
		}*/
		Order order = this.orderService.findOrderByOrderNo(orderNo);
		List<OrderDetail> lDetails = this.orderDetailService.findOrderDetailList(order.getId());
		if(lDetails.size()>0){
			request.setAttribute("lDetails", lDetails);
		}
		return "/mall/business/order/orderProduct";
	}
	
	/**
	 * 处理订单退货申请的跳转
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/returnOrder.do")
	public String returnOrder(HttpServletRequest request,HttpServletResponse response){
		String orderNo = request.getParameter("orderNo");
		Order order = this.orderService.findOrderByOrderNo(orderNo);
		List<OrderDetail> lDetails = this.orderDetailService.findOrderDetailList(order.getId());
		
		//根据订单号去退货申请记录表中查询到这个订单中的退货商品信息
		List<ReturnRequest> rList = this.requestService.findReturnRequests(order.getId());
		if(lDetails.size()>0 && rList.size()>0){
			request.setAttribute("lDetails", lDetails);
			request.setAttribute("rList", rList);
			request.setAttribute("order", order);
		}
		return "/mall/business/order/return-Order";
	}
	
	/**
	 * 同意退货
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/agree.do")
	@ResponseBody
	public String agree(HttpServletRequest request,HttpServletResponse response){
		String orderId = request.getParameter("orderId");
		Order order = this.orderService.findOrderById(Long.parseLong(orderId));
		if(order!=null){
			order.setStatus(8);   //状态8，客服同意退货申请
			try {
				this.orderService.updateOrder(order);
			} catch (Exception e) {
				return "1";
			}
		}
		return "0";   //成功
	}
	
	/**
	 * 拒绝退货
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/reject.do")
	@ResponseBody
	public String reject(HttpServletRequest request,HttpServletResponse response){
		String orderId = request.getParameter("orderId");
		Order order = this.orderService.findOrderById(Long.parseLong(orderId));
		if(order!=null){
			order.setStatus(9);  //状态9，客服拒绝退货申请
			try {
				this.orderService.updateOrder(order);
			} catch (Exception e) {
				return "1";
			}
		}
		return "0";
	}
	
	
	
	/**
	 * 完成操作，需要执行以下功能
	 * 1、记录订单的发货信息
	 * 2、修改订单的状态
	 */
	@RequestMapping("/comfireProduct.do")
	@ResponseBody
	public String comfireProduct(HttpServletRequest request,HttpServletResponse response){
		//接受订单号
		String orderNo = request.getParameter("orderNo");
		
		String expcom = request.getParameter("expcom");
		if(!StringUtils.isEmpty(expcom))expcom = EscapeUtils.unescape(expcom);
		
		String expNo = request.getParameter("expNo");
		if(!StringUtils.isEmpty(expNo))expNo = EscapeUtils.unescape(expNo);
		
		Order od = this.orderService.findOrderByOrderNo(orderNo);
		
		if(od!=null){
			try {
				od.setExpcom(expcom);
				od.setExpNo(expNo);
				od.setExpDate((new Date()).toString());
				od.setStatus(4);    //状态“确定”，表示订单已经完成
				orderService.updateOrder(od);
			} catch (Exception e) {
				return "1";
			}
		}
		return "0";
	}
	
}
